/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
 
db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.util;

import com.db4o.ext.StoredClass;
import com.db4o.reflect.ReflectClass;
import java.util.prefs.Preferences;

/**
 *
 * @author klevgert
 */
public class PreferencesUtil {
  
  private static final String CLASSPATH_DELIM = ";";
  private static final String SUPPRESS_DELIM = " |,|;";

  private static final String KEY_SUPPRESSED_PKGS = "suppressedPackages";
  private static final String KEY_THRESHOLD = "thresholdValue";
  private static final String KEY_DELETE_ON_DEFRAG = "deleteOnDefragmentation";
  private static final String KEY_CLASSPATH = "classpath";
  private static final String KEY_TREE_LIST = "treeList";
  private static final String KEY_SERVER_HOST = "host";
  private static final String KEY_SERVER_PORT = "port";
  private static final String KEY_SERVER_USER = "user";
  private static final String KEY_SERVER_PASSWORD = "password";
  
  private static final String DEFAULT_SUPPRESSED_PKGS = "com.db4o,java,sun,com.sun,org,net,javax";
  private static final String DEFAULT_THRESHOLD = "10";
  private static final String DEFAULT_DELETE_ON_DEFRAG = Boolean.toString(false);
  private static final String DEFAULT_CLASSPATH = "";
  private static final String DEFAULT_USE_TREE_LIST = Boolean.toString(false);
  private static final String DEFAULT_SERVER_HOST = "localhost";
  private static final String DEFAULT_SERVER_PORT = "4488";
  private static final String DEFAULT_SERVER_USER = "db4o";
  private static final String DEFAULT_SERVER_PASSWORD = "db4o";
  
  public static int getThresholdValue() {
    String thresholdValue = 
      Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_THRESHOLD,DEFAULT_THRESHOLD);
    return Integer.parseInt(thresholdValue);
  }

  public static void setThresholdValue(int value) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_THRESHOLD, ""+value);
  }
  
  /**
   * Gets suppressed packages. 
   * Suppressed packages are simply a string concatenated by semicolon.
   * @param suppressed packages string
   */  
  public static String getSuppressedPackages() {
    return Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_SUPPRESSED_PKGS,DEFAULT_SUPPRESSED_PKGS);
  }

  /**
   * Sets suppressed packages. 
   * Suppressed packages are simply a string concatenated by semicolon.
   * @param aSuppressedPackages  suppressed packages string
   */  
  public static void setSuppressedPackages(String aSuppressedPackages) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_SUPPRESSED_PKGS, aSuppressedPackages);
  }

  /**
   * Determines if a package should suppressed from display.
   * 
   * @param storedClass	a stored class to check for a suppressed package.
   * @return true, if the package the class belongs to should be suppressed.
   */
  public static boolean isSuppressedPackage(StoredClass storedClass) {

    return isSuppressedPackage(storedClass.getName());
  }	
  /**
   * Determines if a package should suppressed from display.
   * 
   * @param reflectClass	a reflected class to check for a suppressed package.
   * @return true, if the package the class belongs to should be suppressed.
   */
  public static boolean isSuppressedPackage(ReflectClass reflectClass) {

    return isSuppressedPackage(reflectClass.getName());
  }	

  /**
   * Determines if a package should suppressed from display.
   * 
   * @param storedClass	a stored class to check for a suppressed package.
   * @return true, if the package the class belongs to should be suppressed.
   */
  public static boolean isSuppressedPackage(String className) {

    String[] packages = getSuppressedPackages().split(SUPPRESS_DELIM);
    for (int i = 0; i < packages.length; i++) {
      if( className.startsWith(packages[i]) ) {
        return true;
      }
    }
    return false;
  }

  /**
   * Determines if objects should be deleted on defragmentation.
   * @return true, if objects should be deleted on defragmentation, otherwise false.
   */
  public static boolean isDeleteOnDefragmentation() {
    String deleteOnDefragmentation = 
      Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_DELETE_ON_DEFRAG, DEFAULT_DELETE_ON_DEFRAG);
    return Boolean.parseBoolean(deleteOnDefragmentation);
  }

  /**
   * Sets the flag if objects should be deleted on defragmentation.
   * @param del   true, if objects should be deleted on defragmentation
   */
  public static void setDeleteOnDefragmentation(boolean del) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_DELETE_ON_DEFRAG, Boolean.toString(del));
  }
  
  /**
   */
  public static String getClasspathSettings() {
    return Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_CLASSPATH,DEFAULT_CLASSPATH);
  }

  public static void setServerHost(String host) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_SERVER_HOST, host);
  }
  
  public static String getServerHost() {
    return Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_SERVER_HOST,DEFAULT_SERVER_HOST);
  }
  
  public static void setServerPort(String port) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_SERVER_PORT, port);
  }
  
  public static String getServerPort() {
    return Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_SERVER_PORT, DEFAULT_SERVER_PORT);
  }

  public static void setServerUser(String user) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_SERVER_USER, user);
  }
  
  public static String getServerUser() {
    return Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_SERVER_USER,DEFAULT_SERVER_USER);
  }

  public static void setServerPassword(String password) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_SERVER_PASSWORD, password);
  }
  
  public static String getServerPassword() {
    return Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_SERVER_PASSWORD,DEFAULT_SERVER_PASSWORD);
  }
  
  /**
   */
  public static void setClasspathSettings(String aClasspathSettings) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_CLASSPATH, aClasspathSettings);
  }  
  
  
  
  /**
   * Returns an array of URLs to libraries 
   * (or folders) set in preferences.
   *
   * @return array of URLs, will never be null
   *
   */
  public static String[] getSpecifiedLibraries() {
    String classpath = getClasspathSettings();
    return classpath.split(CLASSPATH_DELIM);
  }

  public static boolean isUseTreeList() {
    String useTreeList = 
      Preferences.userNodeForPackage(PreferencesUtil.class).get(KEY_TREE_LIST, DEFAULT_USE_TREE_LIST);
    return Boolean.parseBoolean(useTreeList);
  }

  public static void setUseTreeList(boolean treeList) {
    Preferences.userNodeForPackage(PreferencesUtil.class).put(KEY_TREE_LIST, Boolean.toString(treeList));
  }

  
}
